<template>
  <div>
    <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
      <div class="widget am-cf">
        <div class="widget-head am-cf">
          <div class="widget-title  am-cf">消耗统计(汇总)</div>
        </div>
        <div class="widget-body  am-fr">
          <div class="am-u-sm-12 am-form ">
            

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.areaTeamId" :options="areaTeams">
                  <option value="">区域</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.year">
                  <option value="">年份</option>
                  <option value="2017">2017</option>
                  <option value="2018">2018</option>
                  <option value="2019">2019</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <select2  v-model="query.periodId" :options="periods">
                  <option value="">期数</option>
                </select2>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <date-picker v-model="query.startDate" >
                  <input type="text" placeholder="请选择开始日期" data-am-datepicker readonly required >
                </date-picker>
              </div>
            </div>

            <div class="am-u-sm-12 am-u-md-12 am-u-lg-3">
              <div class="am-form-group">
                <date-picker v-model="query.endDate" >
                  <input type="text" placeholder="请选择结束日期" data-am-datepicker readonly required >
                </date-picker>
              </div>
            </div>
            <div class="am-u-sm-12 am-u-md-12 am-u-lg-6 am-u-end">
              <div class="am-form-group">
                <button type="button" class="am-btn am-btn-default am-btn-success"
                    @click="search" ><span class="am-icon-search"></span>查询
                </button>
              </div>
            </div>


          </div>

          <div class="am-u-sm-12">
            <el-table
              :data="tableData"
              border
              stripe
              style="min-width: 100%">
              <el-table-column
                prop="consumeYear"
                label="年份"
                min-width="100">
              </el-table-column>
              <el-table-column
                prop="consumeMonth"
                label="月份"
                min-width="100">
              </el-table-column>
              <el-table-column
                label="消耗金额"
                min-width="100">
                <template scope="scope">
                  {{scope.row.consumeAmount | formatNumber(2)}}
                </template>
              </el-table-column>
              <el-table-column
                label="红冲消耗"
                min-width="100">
                <template scope="scope">
                  {{scope.row.redAmount | formatNumber(2)}}
                </template>
              </el-table-column>
              <el-table-column
                label="差额消耗"
                min-width="100">
                <template scope="scope">
                  {{scope.row.differenceAmount | formatNumber(2)}}
                </template>
              </el-table-column>
            <el-table-column
                label="汇总消耗"
                min-width="100">
                <template scope="scope">
                  {{scope.row.total | formatNumber(2)}}
                </template>
              </el-table-column>
            </el-table>
          </div>
          <div class="am-u-lg-12 am-cf">

            <div class="am-fr">
              <pagination v-bind:total="total" v-bind:pageNo="pageNo" v-bind:pageSize="pageSize"
                          @paging="handlePage"/>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
<script>
  import io from '../../lib/io'
  import Pagination from '../base/Pagination.vue'
  import util from '../../lib/util'
  import moment from 'moment'

  export default{
    data: function () {
      return {
        tableData: [],
        allData: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        query: {
          areaTeamId : '',
          startDate : util.formatDate( util.firstDayOfMonth() ),
          endDate : util.formatDate( util.endDayOfMonth() ),
          periodId: '',
          year:"2018"
        },
        products:[],
        periods:[],
      }
    },
    mounted: function () {
      $(window).smoothScroll()
    },
    created: function () {
    //   this.loadTableData(this.pageNo)
      this.loadPeriodData()
    },
    components: {Pagination},
    computed: {
      areaTeams: function () {
        var options = ( this.$root.config.areaTeams || [] )
          .map(function (item) {
            return {value: item.areaTeamId, text: item.name}
          })
            return options
        },
      
    },
    watch: {
        'query.year' (newVal, oldVal) {
            this.loadPeriodData()
        },
        'query.areaTeamId' (newVal, oldVal) {
            this.loadPeriodData()
      }
    },
    methods: {
      loadPeriodData: function () {
        var _this = this
        io.post(io.periodByYearAndAreaTeamId, {
          areaTeamId : this.query.areaTeamId,
          year: this.query.year ? moment(this.query.year).format('YYYY'): '',
        }, function (ret) {
          if (ret.success) {
            _this.periods = ret.data.map(function (item) {
              return {value: item.periodId, text: item.periodName }
            })
            if(!_this.periods.length){
              _this.$alert('当前没有设置期数')
              _this.query.periodId = ''
            }else{
              let curPeriodId = ret.data.filter(item => item.isCurrent == 1 )
              if(curPeriodId.length){
                _this.query.periodId = curPeriodId[0].periodId
              }else{
                _this.query.periodId = ''
              }
              
            }
            
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      search: function () {
        this.loadTableData(1)
      },
      loadTableData: function (pageNo) {
        var _this = this
        _this.pageNo = pageNo || _this.pageNo || 1
        _this.$showLoading()
        let param = {...this.query}
        param.startDate = this.query.startDate+' 00:00:00'
        param.endDate = this.query.endDate+' 23:59:59'
        io.post(io.findConsumeCollect, param, function (ret) {
          _this.$hiddenLoading()
          if (ret.success) {
            _this.total = ret.data.length
            _this.allData = ret.data
            _this.handlePage(pageNo)
          } else {
            _this.$alert(ret.desc)
          }
        })
      },
      handlePage(pageNo) {
        this.pageNo = pageNo || this.pageNo || 1
        this.tableData = this.allData.slice((this.pageNo-1)*this.pageSize,this.pageSize*(this.pageNo))
      },

    }
  }
</script>
